package com.sm.scorpio.ui.fragment.mine;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scorpio.baselibrary.utils.AppDataUtil;
import com.sm.scorpio.R;
import com.sm.scorpio.http.HttpNormalCallback;
import com.sm.scorpio.http.HttpUtils;
import com.sm.scorpio.ui.activity.user.UserInfoActivity;
import com.sm.scorpio.ui.base.BaseFragment;
import com.sm.scorpio.ui.bean.http.UserDataHttpBean;
import com.sm.scorpio.ui.bean.model.UserBean;

import java.util.ArrayList;
import java.util.List;

/**
 * created by zhud on 2019/6/6
 */
public class MineFragment extends BaseFragment implements BaseQuickAdapter.OnItemChildClickListener, View.OnClickListener {
    private MineMenuListAdapter adapter;
    private List<MineMenuItemBean> mDatas = new ArrayList<>();
    private RecyclerView recyclerView;

    @Override
    protected int onCreateLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void onInit(Bundle savedInstanceState) {
        mDatas.add(new MineMenuItemBean(R.drawable.icon_config, R.string.string_00011).setShowRight(true));
        v.setRecyclerViewLayoutManager(R.id.recyclerView, new LinearLayoutManager(mContext))
                .setAdapter(adapter = new MineMenuListAdapter(mDatas));
        adapter.setOnItemChildClickListener(this);
        recyclerView = v.getView(R.id.recyclerView);
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        v.setText(R.id.mine_name_text, "用户昵称");
        updataUserData(null);
        v.addOnClickListener(this, R.id.mine_user_btn);
    }

    @Override
    public void onResume() {
        super.onResume();
        userHttp();
    }

    private void userHttp() {
        HttpUtils.postString().findUserInfo().enqueue(new HttpNormalCallback<UserDataHttpBean>(getActivity()) {
            @Override
            protected void onSuccess(UserDataHttpBean data) {
                UserBean userDataBean = (UserBean) AppDataUtil.getInstance().readData(new UserBean());
                userDataBean.nickname = data.data.nickname;
                userDataBean.sex = data.data.sex;
                AppDataUtil.getInstance().commitData(userDataBean);
                updataUserData(userDataBean);
            }
        });
    }

    private void updataUserData(UserBean data) {
        if (data == null) data = (UserBean) AppDataUtil.getInstance().readData(new UserBean());
        v.setText(R.id.mine_name_text, data.nickname)
                .setImageResource(R.id.mine_head_image, "1".equals(data.sex)
                        ? R.drawable.icon_head_portait_boy : R.drawable.icon_head_portait_girl);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mine_user_btn:
                skipActivity(UserInfoActivity.class);
                break;
        }
    }
}
